﻿
namespace Jayden.Dll.Sc2
{
	public class Rules
	{
		private Rules() { }


		public const int ExtractorPerBase = 2;
		public const int MineralPatchPerBase = 8;
		/// <summary>Amount of minerals harvested in a game minute by a standard worker when working with (maximum) another harvester on the same patch</summary>
		public const int MineralsRate = 42;
		/// <summary>Maximum amount of minerals harvested in a game minute on one patch. (without mule)</summary>
		public const int MineralsMaxRatePerPatch = 102;
		/// <summary>Amount of vespene harvested in a game minute by a standard worker when working with (maximum) another harvester on the same geyser</summary>
		public const int VespeneRate = 38;
		/// <summary>Maximum amount of vespene harvested in a game minute on one geyser</summary>
		public const int VespeneMaxRatePerGeyser = 114;


		public static int GetMineralsSaturationWorkerCount(int patches) { return 3 * patches; }
		public static int GetMineralsOptimizedWorkerCount(int patches) { return 2 * patches; }
		public static int GetGasSaturationWorkerCount(int geysers) { return 3 * geysers; }
		public static int GetGasOptimizedWorkerCount(int geysers) { return 2 * geysers; }
		public static int GetOptimizedWorkerCount(int patches, int geysers) { return GetMineralsOptimizedWorkerCount(patches) + GetGasOptimizedWorkerCount(geysers); }
		public static int GetSaturationWorkerCount(int patches, int geysers) { return GetMineralsSaturationWorkerCount(patches) + GetGasSaturationWorkerCount(geysers); }

		/// <summary>Get the amount of minerals a colony can mine in a game minute.</summary>
		/// <param name="worker">number of workers mining minerals.</param>
		/// <param name="mule">number of mules.</param>
		/// <param name="patches">number of patches in the bases.</param>
		/// <returns>The amount of minerals a colony can mine in a game minute</returns>
		public static int GetMineralsIncome(int worker, int mule, int patches)
		{
			double workerPerPatch = worker / (double)patches;
			int muleMineralsRate = mule * 270;
			if (workerPerPatch <= 2) return muleMineralsRate + worker * MineralsRate;
			if (workerPerPatch >= 3) return muleMineralsRate + patches * MineralsMaxRatePerPatch;
			int count = 0;
			while (worker > patches)
			{
				worker -= patches;
				count++;
			}
			return muleMineralsRate + (patches * count * MineralsRate) + worker * (MineralsMaxRatePerPatch - (2 * MineralsRate));
		}

		/// <summary>Get the amount of vespene gas a colony can gather in a game minute.</summary>
		/// <param name="worker">number of workers harvesting gas.</param>
		/// <param name="geysers">number of geysers in the bases.</param>
		/// <returns>The amount of vespene gas a colony can gather in a game minute</returns>
		public static int GetGasIncome(int worker, int geysers)
		{
			double workerPerPatch = worker / (double)geysers;
			if (workerPerPatch <= 2) return worker * VespeneRate;
			if (workerPerPatch >= 3) return geysers * VespeneMaxRatePerGeyser;
			int count = 0;
			while (worker > geysers)
			{
				worker -= geysers;
				count++;
			}
			return (geysers * count * VespeneRate) + worker * (VespeneMaxRatePerGeyser - (2 * VespeneRate));
		}
	}
}
